package com.euc.springbootframework.dao.pagination;


import com.euc.springbootframework.util.ModelUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

public class BaseConditionQuery<T> {
	protected Pageable pageable=null;
	protected Integer page = -1;
	protected Integer start = 0;
	protected Integer limit = 10;
	protected Sort sort;
	protected SearchFilter[] searchFilters;
	protected Class<T> entityClass;
	
	@SuppressWarnings("unchecked")
	public BaseConditionQuery() {
		entityClass = ModelUtils.getUniquenessGenericClass(getClass());
	}
    
	public SearchFilter[] getSearchFilters() {
		return searchFilters;
	}

	public void setSearchFilters(SearchFilter[] searchFilters) {
		this.searchFilters = searchFilters;
	}

	public void setSort(Sort sort) {
		this.sort = sort;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public Pageable getPageable() {
		if(pageable == null&&page>=0){
			pageable = new PageRequest(page-1, limit , getSort());
		}
		return pageable;
	}

	public void setPageable(Pageable pageable) {
		this.pageable = pageable;
	}
	
	public Sort getSort(){
		if(sort == null){
			return new Sort(Direction.DESC,"_id");
		}else{
			return sort;
		}
		
	}
}
